package de.szut.traineeslife.fragments;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;

import com.google.api.services.drive.Drive;

import de.szut.traineeslife.AuthenticationActivity;
import de.szut.traineeslife.R;
import de.szut.traineeslife.adapter.FilesFromSDAdapter;
import de.szut.traineeslife.taks.PrepareDriveTask;
import de.szut.traineeslife.utils.DialogListener;
import de.szut.traineeslife.utils.DriveHelper;
import de.szut.traineeslife.utils.FileHelper;
import de.szut.traineeslife.utils.ProfilHelper;

public class SelectModelDialogFragment extends DialogFragment {
	private final String TAG = "selectmodeldialog";
	private ListView modelLV;
	private Dialog mDialog;
	private Button finishBtn;
	private Button backBtn;
	private FilesFromSDAdapter fromSdAdapter;
	private DialogListener dialogListener;
	private ArrayList<File> mListOfDoc = new ArrayList<File>();
	private ProfilHelper mProfilHelper;
	private DriveHelper mDriveHelper;
	private FileHelper mFileHelper;
	private PrepareDriveTask prepareDriveTask;
	private Drive service;

	public SelectModelDialogFragment() {
	}

	/**Build the Dialog to Select a Model from choosen Folder
	 * @param dialog
	 * @param profilHelper
	 * @param driveHelper
	 * @param fileHelper
	 * @param listOfDoc
	 */
	public SelectModelDialogFragment(Dialog dialog, ProfilHelper profilHelper, DriveHelper driveHelper, FileHelper fileHelper, ArrayList<File> listOfDoc) {
		mDialog = dialog;
		mProfilHelper = profilHelper;
		mDriveHelper = driveHelper;
		mFileHelper = fileHelper;
		mListOfDoc = listOfDoc;
	}

	public static SelectModelDialogFragment newInstance(Dialog dialog, ProfilHelper profilHelper, DriveHelper driveHelper, FileHelper fileHelper, ArrayList<File> listOfDoc) {
		SelectModelDialogFragment dialogFragment = new SelectModelDialogFragment(dialog, profilHelper, driveHelper, fileHelper, listOfDoc);
		return dialogFragment;

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		service = AuthenticationActivity.getService();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.dialog_select_model_list, container, false);
		mDialog = getDialog();
		mDialog.setTitle(R.string.select_dialog);
		mDialog.setCancelable(false);
		mDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
		mDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		modelLV = (ListView) v.findViewById(R.id.select_dlg_list);
		fromSdAdapter = new FilesFromSDAdapter(getActivity(), 0, mListOfDoc);
		modelLV.setAdapter(fromSdAdapter);
		modelLV.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
				File model = mListOfDoc.get(position);
				String filePath = model.getAbsolutePath();
				Uri fileUri = null;
				mProfilHelper.setPathOfModel(filePath);
				for (String endig : mFileHelper.getEndings()) {
					if (model.getPath().toLowerCase().endsWith(endig)) {
						fileUri = Uri.fromFile(new java.io.File(filePath));
					}
				}
				if (fileUri != null) {
					prepareDriveTask = new PrepareDriveTask(getActivity(), service, mDriveHelper, fileUri, mFileHelper, dialogListener);
					prepareDriveTask.execute();
				}
			}
		});
		return v;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (!(activity instanceof DialogListener)) {
			throw new RuntimeException("Activity must implement WelcomeDialogListener");
		}
		dialogListener = (DialogListener) getActivity();
	}

	public String getTAG() {
		return TAG;
	}

}
